package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.constant.RedisConstant;
import com.itheima.health.dao.*;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.*;
import com.itheima.health.service.MobileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Random;

@Service
public class MobileServiceImpl implements MobileService {
    @Autowired
    private MobileDto mobileDto;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private CheckgroupCheckitemDao checkgroupCheckitemDao;
    @Autowired
    private SetmealCheckgroupDao setmealCheckgroupDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 手机端_体检预约_套餐展示
     */
    @Override
    public Result getSetmeal() {
        List<Setmeal> list = mobileDto.selectSetmeal();
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS,list);
    }

    /**
     * 根据id查询套餐详情
     */
    @Override
    public Result findById(String id) {
        //根据id查询套餐
       Setmeal setmeal =  mobileDto.findById(id);
            //根据id查询套餐对应的查询组的id
            List<Integer> checkGroupIds = setmealCheckgroupDao.selectId(id);
            //根据id查询查询组
            List<CheckGroup> groups= checkGroupDao.selectByids(checkGroupIds);
            //循环将将查项集合添加到检查组中
        for (CheckGroup group : groups) {
            //根据检查组的id查询检查项id
            List<Integer> itemIds= checkgroupCheckitemDao.selectByGroupId(group.getId());
            //根据检查项id查询检查项
            List<CheckItem> checkItems =checkItemDao.selectByIds(itemIds);
            //封装
            group.setCheckItems(checkItems);
        }
        //封装
        setmeal.setCheckGroups(groups);
        return new Result(true,MessageConst.QUERY_SETMEALLIST_SUCCESS,setmeal);
    }

    /**
     *获取验证码
     */
    @Override
    public Result send(String type, String telephone) {
        //使用random生成一个随机数
        Random random = new Random();
        //生成一个0到9999之间的随机数
        String rand = "";
        for (int i = 0; i < 6; i++) {
            int randomNumber = random.nextInt(10);
            //将随机数转换成字符串
            String s = String.valueOf(randomNumber);
            rand = rand +s;
        }
        //将字符串随机数存入都按Redis中
        redisTemplate.opsForValue().set(RedisConstant.ORDER,rand);
        redisTemplate.opsForValue().set(RedisConstant.MOBILE_SIGNIN,rand);
        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     *手机端验证码登录
     */
    @Override
    public Result login(SmsLogin smsLogin) {
        String value = redisTemplate.opsForValue().get(RedisConstant.MOBILE_SIGNIN);
        if( !smsLogin.getValidateCode().equals(value)){
            return new Result(false,MessageConst.ACTION_FAIL);
        }
        Member member = new Member();
        member.setPhoneNumber(smsLogin.getTelephone());
        int count =memberDao.select(member);
        if(count == 0){
            memberDao.inset(member);
        }
        return new Result(true,MessageConst.LOGIN_SUCCESS);
    }

    @Override
    public Result sumbit(Submit submit) {
        String value = redisTemplate.opsForValue().get(RedisConstant.ORDER);
        if(submit.getValidateCode().equals(value)){
            Order order = orderDao.select(submit);
            return new Result(true,MessageConst.ACTION_SUCCESS,order);
        }
        return new Result(false,MessageConst.VALIDATECODE_ERROR);
    }
}
